package com.approval.approvalapp.index.dbsx.wsys;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.SbclckBean;
import com.approval.approvalapp.bean.WsysBjxq;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.SbclckActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WsysXqActivity extends BaseActivity2 {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.sbsx_tv)
    TextView sbsxTv;
    @Bind(R.id.sqr_tv)
    TextView sqrTv;
    @Bind(R.id.sbsj_tv)
    TextView sbsjTv;
    @Bind(R.id.blsx_tv)
    TextView blsxTv;
    @Bind(R.id.lxdh_tv)
    TextView lxdhTv;
    @Bind(R.id.zjlx_tv)
    TextView zjlxTv;
    @Bind(R.id.zjhm_tv)
    TextView zjhmTv;
    @Bind(R.id.sbclck_ll)
    LinearLayout sbclckLl;
    @Bind(R.id.ystg_tv)
    TextView ystgTv;
    @Bind(R.id.ysbh_tv)
    TextView ysbhTv;
    private List<WsysBjxq.RecordBean> record;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wsys_xq;
    }

    @Override
    public void initView() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("projID", getIntent().getStringExtra("projID"));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/int_show",
                jsonObject.toString(), new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        Log.d("WsysXqActivityss", str);
                        WsysBjxq wsysBjxqs = JSON.parseObject(str, WsysBjxq.class);
                        record = wsysBjxqs.getRecord();
                        if (titleTv1 == null){
                            return;
                        }
                        titleTv1.setText("网上预审");
                        sbsxTv.setText("申报事项: " + wsysBjxqs.getITEM_NAME());
                        sqrTv.setText("申请人: " + wsysBjxqs.getAPPLICANT());
                        sbsjTv.setText("申报时间: " + getIntent().getStringExtra("SUBMIT_TIME"));
                        blsxTv.setText(wsysBjxqs.getITEM_NAME());
                        lxdhTv.setText(wsysBjxqs.getCONTACT_PHONE());
                        zjlxTv.setText("身份证");
                        zjhmTv.setText(wsysBjxqs.getIDCARD_NO());
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back_iv, R.id.ystg_tv, R.id.ysbh_tv, R.id.sbclck_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.sbclck_ll:
                List<SbclckBean> sbclckBeanList = new ArrayList<>();
                if (record == null) {
                    return;
                }
                for (int i = 0; i < record.size(); i++) {
                    sbclckBeanList.add(new SbclckBean(record.get(i).getFILE_NAME(),
                            record.get(i).getFILE_PATH(),record.get(i).getFILE_URL()));
                }
                SbclckActivity.goInto(this, sbclckBeanList);
                break;
            case R.id.ystg_tv:
                ystg = true;
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomEditTextBottomPopup(this))
                        .show();
                break;
            case R.id.ysbh_tv:
                ystg = false;
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomEditTextBottomPopup(this))
                        .show();
                break;
        }
    }

    boolean ystg;

    private class CustomEditTextBottomPopup extends BottomPopupView {
        public CustomEditTextBottomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_edittext_bottom_popup;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
        }

        @Override
        protected void onShow() {
            super.onShow();
            findViewById(R.id.btn_finish).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            findViewById(R.id.hf_tv).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(getComment())) {
                        dismiss();
                        Toast.makeText(WsysXqActivity.this, "输入内容为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //提交之后刷新页面 隐藏按钮
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("projID", getIntent().getStringExtra("projID"));
                    if (ystg){
                        jsonObject.put("checkResult", "1");
                    }else {
                        jsonObject.put("checkResult", "0");
                    }
                    jsonObject.put("opinion", getComment());
                    jsonObject.put("handlerName", SpUtil.getString(WsysXqActivity.this,SpUtil.USER_RUN_NICKNAME,""));
                    jsonObject.put("deptID", SpUtil.getString(WsysXqActivity.this,SpUtil.ORG_CODE,""));
                    jsonObject.put("deptName", SpUtil.getString(WsysXqActivity.this,SpUtil.UNIT_NAME,""));
                    dismiss();
                    NetRequsetUtil.getInstance().netRequestPostJson("v1/user/int_pass", jsonObject.toString(),
                            new NetDownResponse() {
                                @Override
                                public void success(String str) {
                                    if (WsYsActivity.instance != null){
                                        WsYsActivity.instance.refreshData();
                                    }
                                    Toast.makeText(WsysXqActivity.this, "预审完成", Toast.LENGTH_SHORT).show();
                                    WsysXqActivity.this.finish();
                                }

                                @Override
                                public void errowithresponse(String str) {

                                }

                                @Override
                                public void erro() {
                                    ErrorBeanUtil.getErrorMessage(null);
                                }

                                @Override
                                public void finish() {

                                }
                            });
                }
            });
        }


        @Override
        protected void onDismiss() {
            super.onDismiss();
            Log.e("tag", "CustomEditTextBottomPopup  onDismiss");
        }

//

        public String getComment() {
            EditText et = findViewById(R.id.et_comment);
            return et.getText().toString();
        }
    }
}
